package cn.com.jrr.core.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.common.WebKey;
import cn.com.jrr.core.anotation.SystemControllerLog;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.ResponseMessage;
import cn.com.jrr.core.model.Vote;
import cn.com.jrr.core.model.VoteDetail;
import cn.com.jrr.core.model.power.SysUser;
import cn.com.jrr.core.service.VoteService;

@Controller
@RequestMapping("/vote")
public class VoteAction {
    @Autowired
    private VoteService voteService;
    private Vote vote;
    @Autowired
    private ResponseMessage responseMessage;
    private DataTableResponse<Vote> dt;
    /**
     * 初始化
     *
     */

    @RequestMapping("/main")
    public String info(){
        return "/vote/votelist";
    }


    /**
     * 查询全部
     */
    @ResponseBody
    @RequestMapping("/list")
    @SystemControllerLog(description="查询消息")
    public DataTableResponse<Vote> list(DataTableRequest dataTableRequest){
        dt=voteService.getAll(dataTableRequest);
        return dt;
    }
    /**
     * 添加信息
     */
    @ResponseBody
    @RequestMapping("/saveorupdate")
    @Transactional
    @SystemControllerLog(description="添加信息")
    public ResponseMessage saveOrUpadate(Vote vote,HttpServletRequest request){
        if(vote==null){
            responseMessage.setMessage("添加失败 ");
            return responseMessage;
        }
        try{
            if(vote.getId()!=null){
                vote.setUpdatedt(new Date());
                vote.setUpdateid(((SysUser)request.getSession().getAttribute(WebKey.USER)).getUserName());
                voteService.update(vote);
                responseMessage.setStatus("success");
            }else{
                vote.setEntryid(((SysUser)request.getSession().getAttribute(WebKey.USER)).getUserName());
                vote.setEntrydt(new Date());
                voteService.add(vote);
                responseMessage.setStatus("success");
            }
        }catch(Exception e){
            responseMessage.setStatus("fail");
        }
        return responseMessage;
    }

    /**
     * 获取一个
     */
    @ResponseBody
    @RequestMapping("/get")
    public Vote get(@RequestParam Integer id){
        vote=voteService.get(id);
        return vote;
    }
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public ResponseMessage remove(@RequestParam String idString){
        List<Integer> idList=ToolUtils.splitIdString(idString);
        for(Integer id:idList){
            voteService.delete(id);
        }
        responseMessage.setStatus("success");
        return responseMessage;
    }
    /**
     * 设置URL
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/seturl")
    public ResponseMessage setUrl(Integer id,HttpServletRequest request) throws IOException{
        String url=request.getScheme()+"://"+request.getLocalAddr()+":"+request.getServerPort()+ request.getContextPath() + "/front?voteid="+id;
        try{
            voteService.setUrl(url, id);
            responseMessage.setStatus("success");
        }catch(Exception e){
            responseMessage.setStatus("fail");
        }
        return responseMessage;
    }
	/*C:\Windows\System32\drivers\etc\hosts中
	　　有一行，如下，将localhost映射为了ipv6格式的本机ip地址：
	::1 localhost
	解决方案1：
	　　在其前面添加一个“#”注释掉
	　　或者改成
	127.0.0.1 localhost
	解决方案2：
	　　访问页面的时候不要用http://localhost:8080/...，使用http://127.0.0.1:8080/...即可*/

}
